package com.wyh.controller.gfive;

import com.wyh.common.result.Result;
import com.wyh.domain.dto.gfive.RoleAddDTO;
import com.wyh.domain.dto.gfive.RoleUpdateDTO;
import com.wyh.domain.po.gfive.PageHelper;
import com.wyh.domain.po.gfive.RoleStatusUpdateRequest;

import com.wyh.service.gfive.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "系统管理-角色管理")
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/findAll")
    @ApiOperation("查询所有角色")
    public Result findAll(@RequestParam(required = false) @ApiParam("页数") Integer currentPage,
                          @RequestParam(required = false) @ApiParam("条数") Integer pageSize) {
        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);
        return roleService.selectRole(null,null,pageHelper);
    }

    @GetMapping("selectRole")
    @ApiOperation("根据名字或状态查询角色")
    public Result selectRole(@ApiParam("角色名") String name, @ApiParam("角色状态 0停用 1启用") Integer state,
                             @RequestParam(required = false) @ApiParam("页数") Integer currentPage,
                             @RequestParam(required = false) @ApiParam("条数") Integer pageSize) {
        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);
        return roleService.selectRole(name, state,pageHelper);
    }

    @ApiOperation("修改角色信息")
    @PostMapping("updateRole")
    public Result updateRole(@RequestBody @ApiParam(value = "修改角色DTO，包含角色ID、角色名、角色权限Id",required = true)
                                 RoleUpdateDTO roleUpdateDTO) {
            return roleService.updateRole(roleUpdateDTO);
    }


    @ApiOperation("修改角色状态")
    @PostMapping("updateState")
    public Result updateState(@RequestBody @ApiParam(value = "传入角色ID、角色状态")  RoleStatusUpdateRequest request) {
        return roleService.updateRoleState(request);
    }

    @ApiOperation("添加角色")
    @PostMapping("/add")
    public Result<String> addRole(@RequestBody @ApiParam(value = "传入角色姓名、角色权限", required = true)
                                      RoleAddDTO roleAddDTO) {
            return roleService.addRoleWithPermissions(roleAddDTO);
    }

    @GetMapping("/findPerms")
    public Result findRolePermission(@RequestParam int roleId){
        return roleService.findRolePermission(roleId);
    }
}
